//
//  NSNumber+Addition.m
//  Project
//
//  Created by  CarlsonLee on 2018/12/7.
//  Copyright © 2018年 YANJING. All rights reserved.
//

#import "NSNumber+Addition.h"

@implementation NSNumber (Addition)

+ (NSNumber *)gel_numberWithString:(NSString *)string {
    NSString *str = [[string gel_stringByTrim] lowercaseString];
    if (!str || !str.length) {
        return nil;
    }
    
    // hex number
    int sign = 0;
    if ([str hasPrefix:@"0x"]) sign = 1;
    else if ([str hasPrefix:@"-0x"]) sign = -1;
    if (sign != 0) {
        NSScanner *scan = [NSScanner scannerWithString:str];
        unsigned num = -1;
        BOOL suc = [scan scanHexInt:&num];
        if (suc)
            return [NSNumber numberWithLong:((long)num * sign)];
        else
            return nil;
    }
    
    // normal number
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
    return [formatter numberFromString:string];
}

- (NSString *)gel_wrappedDescription {
    if (self.longLongValue <= 9999) {
        return self.description;
    } else {
        return [NSString stringWithFormat:@"%.1f万",(self.longLongValue)/(10000.0)];
    }
}

- (NSString *)gel_toDecimalStyleString
{
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
    return [numberFormatter stringFromNumber:self];
}

@end
